#include "game.h"
#include "ui_game.h"


game::game(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::game)
{
    ui->setupUi(this);
    this->setGeometry(QRect(300,150,700,500));//窗口大小
    this->setWindowTitle("打地鼠王源版");//窗口标题
    this->setWindowIcon(QIcon(":/images/images/tubiao.jpg"));//窗口图标

    score=0;
    game_flag=0;
    //把图片放入老鼠按钮中
    ui->pushButton->setStyleSheet("QPushButton{border-image:url(:/images/images/after.jpg)}");
    ui->pushButton->hide();//游戏未开始时老鼠不显示
    //游戏未开始时暂停功能不可用
    ui->pushButton_3->setEnabled(false);

    //改变鼠标样式
    QCursor cCursor;
    QPixmap cMap(":/shubiao/shubiao/shubiao.png");
    QSize cSize(60,60);
    QPixmap cScaleMap=cMap.scaled(cSize,Qt::KeepAspectRatio);
    cCursor=QCursor(cScaleMap);
    setCursor(cCursor);
    QCursor cCursor2;//鼠标在普通按钮上变回普通样式
    ui->lcdNumber->setCursor(cCursor2);
    ui->pushButton_2->setCursor(cCursor2);
    ui->pushButton_3->setCursor(cCursor2);
    ui->pushButton_4->setCursor(cCursor2);



}

game::~game()
{
    delete ui;
}
void game::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    //画游戏的背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::lightGray);
    painter.drawRect(0,0,700,500);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(0,0,550,500);
    painter.drawPixmap(0,0,550,500,QPixmap(":/images/images/back.jpg"));
}
//计时器事件
void game::timerEvent(QTimerEvent *){
    if(game_flag==1){
    ui->pushButton->move(QRandomGenerator::global()->bounded(0,400),QRandomGenerator::global()->bounded(0,370));//随机出现
    }
    killTimer(timerNum);
    timerNum=startTimer(1000);
}

//老鼠按钮的槽函数
void game::on_pushButton_clicked()
{
    killTimer(timerNum);
    timerNum=startTimer(1000);
    if(game_flag==1){
    this->score+=10;//加分
    ui->lcdNumber->display(this->score);
    }
}


void game::on_pushButton_2_clicked()//开始按钮
{
    ui->pushButton->show();
    game_flag=1;
    score=0;
    ui->lcdNumber->display(score);
    ui->pushButton_3->setEnabled(true);

}


void game::on_pushButton_3_clicked()//暂停按钮
{
    if(game_flag){
        game_flag=0;
    }
    else{
        game_flag=1;
    }

}


void game::on_pushButton_4_clicked()//结束按钮
{
    game_flag=0;
    if(score>50&&score<=100){
        QMessageBox::about(this," ","Do a good job!");
    }
    else if(score>100){
        QMessageBox::about(this," ","You are excellent!");
    }
    this->score=0;
    ui->lcdNumber->display(this->score);
    ui->pushButton->hide();
    ui->pushButton_3->setEnabled(false);


}

